package com.hns.phone.maintenance.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.mainte.entity.CommLogFileRcdEntity;
import com.hns.can.mainte.service.LoadFileService;
import com.hns.can.mainte.vo.EngineRunStatusParamVo;
import com.hns.can.mainte.vo.LoadFileParamVo;
import com.hns.phone.maintenance.service.PhoneEngineManageService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by JeffXu on 2016-10-11.
 */
@Controller
@RequestMapping("/api/phoneEngineManage")
public class PhoneEngineManageController extends BaseController {

    @Autowired
    private LoadFileService loadFileService;

    @Autowired
    private PhoneEngineManageService phoneEngineManageService;

    /**
     * 查询时间段内的文件列表
     *
     * @param {carIds, componentType = 01, beginTime, endTime}
     * @param params
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/getLoadFileRcd")
    @ResponseBody
    public Object searchLoadFileRcd(LoadFileParamVo params,
                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
        //下发、记录文件加载日志
        loadFileService.saveLoadFile(params);

        //查询文件加载日志表
        List<CommLogFileRcdEntity> commLogFileRcdEntityList = loadFileService.getCommLogFileRcdByTimeList(params);

        return this.getReturnResult(commLogFileRcdEntityList);
    }

    /**
     * 文件下载，通讯将数据入库
     *
     * @param {carIds, fileNames, componentType = 01}
     * @param entity
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/getLoadFiles")
    @ResponseBody
    public Object getLoadFiles(LoadFileParamVo params,
                               HttpServletRequest request, HttpServletResponse response) throws Exception {
        //下载文件放在FTP目录位置
        String ftpEngInfoPath = UtilConfig.getString("ftp_engInfo_path");
        params.setFtpUrl(ftpEngInfoPath);

        loadFileService.loadFiles(params);
        return this.getReturnResult();
    }

    /**
     * 查询下载文件的状态
     *
     * @param {fileNames, carIds, componentType = 01, beginTime, endTime}
     * @param entity
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/getLoadFileSt")
    @ResponseBody
    public Object getLoadFileSt(LoadFileParamVo params,
                                HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<CommLogFileRcdEntity> commLogFileRcdEntityList = null;
        if (PubUtil.isNotEmpty(params.getFileNames())) {
            String devId = loadFileService.getDevId(params);
            params.setDevId(devId);
            commLogFileRcdEntityList = loadFileService.getCommLogFileRcdByFileList(params);
        }
        return this.getReturnResult(commLogFileRcdEntityList);
    }

    /**
     * 查询发动机运行状态数据
     *
     * @param {orgIds,    type, beginTime, endTime}
     * @param conditionVo
     * @return
     * @throws Exception
     */
    @RequestMapping("/getEngineRunStatusList")
    @ResponseBody
    private Object getEngineRunStatusList(EngineRunStatusParamVo conditionVo) throws Exception {
        SysUserEntity user = this.getPhoneCurrentUser();
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setCorpId(user.getCorpId());
        conditionVo.setDriverRelTableName("T_COMM_ENGI_ST_INFO");

        List<BaseEntity> list = phoneEngineManageService.searchEngineRunStatusList(conditionVo);

        return this.getReturnResult(list);
    }


}
